module Kernel {

    typedef UInt8 Priority;
    typedef UInt8 Signal;

    typedef Void (*Reactor)(Ptr, Signal);

    @Opaque struct Fiber {
        List.Desc* eventList;
        Reactor reactor;
    }

    @Opaque struct Event {
        List.Elem elem;
        UInt8 offset;
        Signal signal;
    };

    config Int numPriorities = 2;

    Void initFiber( Fiber* fiber, Priority priority );
    Void initEvent( Event* event, UInt8 offset );

    metaonly Void initFiber_meta( Fiber* fiber, Priority priority, Reactor reactor = null );
    metaonly Void initEvent_meta( Event* event, UInt8 offset, Signal signal = 0 );

    Void setReactor( Fiber* fiber, Reactor reactor );
    Void setSignal( Event* event, Signal signal );

    Void post( Event* event );
    Void start();

internal:

    Void dispatch();
    List.Desc* maxEventList();
    Fiber* toFiber( Event* event );

    struct Module_State {
        List.Desc* curEventList;
        List.Desc eventList[];
    };
}
